import 'package:flutter/material.dart';
import 'package:x3_base_package/x3_base_package.dart';
import 'package:x3_business_core/business_customer/page/business_customer_details_service_ht_details.dart';

class BusinessCustomerDetailsServiceHtTab extends StatefulWidget {

  BusinessCustomerDetailsServiceHtTab({Key key, @required this.vipId, @required this.topicTypeId, @required this.inputApi}) : super(key: key);

  final String vipId;

  final String topicTypeId;

  final String inputApi;

  @override
  _BusinessCustomerDetailsServiceHtTabState createState() => _BusinessCustomerDetailsServiceHtTabState();

}

class _BusinessCustomerDetailsServiceHtTabState extends State<BusinessCustomerDetailsServiceHtTab> with AutomaticKeepAliveClientMixin {

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return BaseRefreshComponent(
      path: widget.inputApi,
      params: {'vipId': widget.vipId, 'topicTypeId': widget.topicTypeId},
      buildItem: ( Map<String, dynamic> item, int index ) => BaseCardComponent(child: _buildContentHtListItemWidget( context, item )),
      controller: BaseRefreshController(),
    );
  }

  Widget _buildContentHtListItemWidget( BuildContext context, dynamic item ) {
    double _htSize = 112;
    String filePath = BaseUtils.getKeyData(item, 'filePath').toString();
    return InkWell(
      onTap: () => _goDepotVipDetailsHtDetails(item),
      child: Row(
        children: [
          BaseImageComponent(imageUrl: filePath, width: _htSize, height: _htSize * (3 / 4), fit: BoxFit.cover, circular: 2),
          SizedBox(width: 12),
          Expanded(child: _buildContentHtListItemContent(context, item)),
        ],
      ),
    );
  }

  Widget _buildContentHtListItemContent(BuildContext context, dynamic item) {
    double _htSize = 112;
    String createDate = BaseTransformUtil.stringToDate(BaseUtils.getKeyData(item, 'createDate').toString());
    String browseNums = BaseUtils.getKeyData(item, 'browseNums').toString();
    TextStyle textStyle = Theme.of(context).textTheme.subtitle2;
    return Container(
      padding: const EdgeInsets.only(left: 4.0),
      height: _htSize * (3 / 4),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _buildContentHtRichText( context, item ),
          BaseComponentUtils.buildTextSpanWidget([createDate, '浏览$browseNums次'], styles: { 0: textStyle, 1: textStyle }),
        ],
      ),
    );
  }

  Widget _buildContentHtRichText( BuildContext context, dynamic item ) {
    String topicTitle = BaseUtils.getKeyData(item, 'topicTitle').toString();
    return RichText(
      softWrap: true,
      maxLines: 2,
      overflow: TextOverflow.ellipsis,
      text: TextSpan(style: Theme.of(context).textTheme.bodyText2, children: [
        _buildContentHtListItemLabel( context, item ),
        WidgetSpan(child: SizedBox(width: 8.0)),
        TextSpan(text: topicTitle, style: Theme.of(context).textTheme.bodyText2.copyWith(fontSize: 14)),
      ]),
    );
  }

  WidgetSpan _buildContentHtListItemLabel( BuildContext context, dynamic item ) {
    String topicTypeName = BaseUtils.getKeyData(item, 'topicTypeName').toString().trim();
    return BaseUtils.isNotEmpty( topicTypeName ) ? WidgetSpan(
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 4, vertical: 1),
        color: Colors.red.shade50,
        child: Text(topicTypeName.toString().trim(), style: Theme.of(context).textTheme.subtitle2.copyWith(color: Colors.red)),
      ),
    ) :  WidgetSpan(child: SizedBox());
  }

  void _goDepotVipDetailsHtDetails( dynamic item ) {
    String topicId = BaseUtils.getKeyData(item, 'topicId').toString();
    Map<String, dynamic> params = { 'topicId': topicId };
    BaseRouterUtil.getInstance().pushNamed(BusinessCustomerDetailsServiceHtDetails.sName, arguments: params);
  }

  @override
  bool get wantKeepAlive => true;

}
